package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.Administrator;
import com.eece496.Database.Course;
import com.eece496.Helpers.CourseInfo;

/**
 * Servlet implementation class ProfAddCourseServlet
 */
public class ProfAddCourseServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ProfAddCourseServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Connect();

		Administrator AdminDB = new Administrator();
		AdminDB.Setup(connection);

		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String Course = request.getParameter("Course");
		String Title = request.getParameter("Title");
		String Section = request.getParameter("Section");
		String Year = request.getParameter("Year");
		String Term = request.getParameter("Term");
		String Instructor = request.getParameter("Instructor");
		int termYear = 0;
		Course CourseDB = new Course(Course, Title, Section, termYear, Term,
				Instructor);

		String isEmpty = null;
		try {
			termYear = Integer.parseInt(Year);

			// Check the length of the Course
			if (Course.length() > 8 || Section.length() > 3) {
				isEmpty = "true";
				request.setAttribute("firstname", firstname);
				request.setAttribute("lastname", lastname);
				request.setAttribute("isEmpty", isEmpty);
				System.out.println("input is not an integer.");
				request.getRequestDispatcher(
						"/Professor/Professor-add-course.jsp").forward(request,
						response);
				
			//Ok we passed this check!
			} else {

				CourseDB.Setup(connection);
				CourseDB.Insert();

				isEmpty = "false";
				ArrayList<CourseInfo> info = new ArrayList<CourseInfo>();

				info = CourseDB.GetAllCourseInfo();

				request.setAttribute("firstname", firstname);
				request.setAttribute("lastname", lastname);
				request.setAttribute("CourseInfo", info);
				request.setAttribute("isEmpty", isEmpty);

				if (info.size() == 0) {
					isEmpty.equals("true");
					System.out.println(isEmpty);
				}

				request.getRequestDispatcher("/Professor/Professor-Portal.jsp")
						.forward(request, response);
			}
		} catch (NumberFormatException e) {
			isEmpty = "true";
			request.setAttribute("firstname", firstname);
			request.setAttribute("lastname", lastname);
			request.setAttribute("isEmpty", isEmpty);
			System.out.println("input is not an integer.");
			request.getRequestDispatcher("/Professor/Professor-add-course.jsp")
					.forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// /request.getRequestDispatcher("index-error.jsp").forward(request,
			// / response);
		}
	}
}
